$(function () {
    const PT_SUBJECTTYPE = 1;
    const PM_SUBJECTTYPE = 2;
    const MALL_SUBJECTTYPE = 3;
    function RoleEditor() {
        this.id = $('#roleId').val();
        // 表格ID
        this.$form = $("#save-form");
        // 按钮
        this.$submit = $('#submit');
        // 初始化
        this.init();
    }

    RoleEditor.prototype.Constructor = RoleEditor;

    RoleEditor.prototype.init = function () {
        this.registEvent();
    };

    RoleEditor.prototype.registEvent = function () {
        var that = this;

        $('#name').blur(function () {
            var name = $.trim($('#name').val());
            if (!name) {
                return false;
            }
            SysTool.Ajax.ajaxJsonCall('roles/exists', {roleId: that.id, name: name}, function (b) {
                if (!!b) {
                    ('#name').val('');
                    $('#name').focus();
                    BootBox.warning('角色名已经存在，请重新输入！');
                    return false;
                }
            });
        });

        $('#type').change(function () {
            var self = $(this);
            var type = self.val();
            _listResource(type);
        });

        this.$submit.click(function () {
            var name = $.trim($('#name').val());
            if (!name) {
                $('#name').focus();
                BootBox.warning('请输入角色名！');
                return false;
            }
            if (name.length > 10) {
                $('#name').focus();
                BootBox.warning('角色名称最多只能为10个字符！');
                return false;
            }

            SysTool.Ajax.ajaxJsonCall('roles/exists', {roleId: that.id, name: name}, function (b) {
                if (!!b) {
                    ('#name').val('');
                    $('#name').focus();
                    BootBox.warning('角色名已经存在，请重新输入！');
                    return false;
                }
                var type = $('#type').val();
                if (!type) {
                    $('#type').focus();
                    BootBox.warning('请选择类型！');
                    return false;
                }

                var checked = $('input[name=resource]:checked');
                if (checked) {
                    var resIds = checked.map(function () {
                        return $(this).attr('resid');
                    }).get().join(",");
                    $('#resIds').val(resIds);
                }

                SysTool.Ajax.ajaxSubmitForm(that.$form, function (data) {
                    BootBox.success('保存成功', function () {
                        window.location.href = "/roles/home";
                    });
                });
            });
        });
    };

    window.RoleEditor = new RoleEditor();
    _listResource($('#type').val());

    function _listResource(type) {
        var resTable = $('#res-table');
        resTable.find('tr').addClass('hide');
        $('#tips').addClass('hide');
        if (!type) {
            resTable.addClass('hide');
            $('#tips').removeClass('hide');
            return false;
        }
        var destSubjectType = [];
        if (type == PT_SUBJECTTYPE) {
            destSubjectType = [1, 6, 8, 12];
        } else if (type == PM_SUBJECTTYPE) {
            destSubjectType = [2, 6, 10, 12];
        } else if (type == MALL_SUBJECTTYPE) {
            destSubjectType = [3, 8, 10, 12];
        }
        if (destSubjectType.length > 0) {
            resTable.removeClass('hide');
            for (var i = 0; i < destSubjectType.length; i++) {
                var t = destSubjectType[i];
                resTable.find('input[subjecttype=' + t + ']').parents('tr').removeClass('hide');
            }
        }
    }
});